package com.intel.factorytest.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import com.intel.factorytest.R;
import java.util.ArrayList;

public class SARSensor extends BaseActivity implements SensorEventListener{

    private final String TAG = "SARSensor";

    private final static int TYPE_SAR = 33171015;
    private SensorManager sm = null;
    private Sensor mSARsensor = null;
    private TextView mTextView = null;

    
    private boolean is_pass;
    private boolean read_0=false ,read_1=false;
    private int count_0 =0,count_1=0;
    private boolean isFirst =true;
    private int temp =0;
    ArrayList<String> valueList;
    
    public void onCreate(Bundle b) {
        super.onCreate(b);
        
        valueList = new ArrayList<>();
        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSARsensor = sm.getDefaultSensor(TYPE_SAR);    
   
        setContentView(R.layout.sar_sensor_test);
        mTextView = findViewById(R.id.sar_sensor_text);
        mTextView.setText("Distance : 0.0");
        if(mSARsensor == null) {
            mTextView.setText(R.string.SERVICEMENU_SAR_SENSER_NULL_TXT);
        }
        super.initView();
    }
    public void onResume(){
        super.onResume();
        sm.registerListener(this, mSARsensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
    public void onPause(){
        super.onPause();
        sm.unregisterListener(this);
    }

    

    @Override
    public void onSensorChanged(SensorEvent event) {
        String message = "";

        if(event.sensor.getType() == TYPE_SAR) {

            //StringBuilder output = new StringBuilder(50);
            Log.d(TAG, "Sensor name is "+ event.sensor.getName());
            
            if(String.valueOf(event.values[0]) == null){
                //Log.d(“wmx”, Sensor values is "+ event.values[0]);
                return;
            }
        
            if(isFirst){
                temp =(int)event.values[0];
            }
            int value = (int)event.values[0];
            if(temp  ==value&& value==0 && !read_0){
                count_0++;
                if(count_0>5){
                    read_0 = true;
                    for(int i=0;i<5;i++){
                        valueList.add("0");
                    }
                }
            }
            
            if(temp  ==value&& value==1  && !read_1){
                count_1++;
                if(count_1>5){
                    read_1 = true;
                    for(int i=0;i<5;i++){
                        valueList.add("1");
                    }
                }
            }
            temp = value;
            if(read_1&& read_0){
                sm.unregisterListener(this);
                TestResult();
            }
            /*for(int i = 0; i < event.values.length; i++ ) {
                output.append(event.sensor.getName()+"\n");
                output.append(event.values[i]);
            }*/
            //if(event.values[0] != 0){
                is_pass = true;
                //message = "Point : " + event.values[0];
                //TestResult(message);
                //System.out.println(message);
                if(valueList == null){
                    valueList = new ArrayList<>();
                }
                
                mTextView.setText("Distance : " + event.values[0]);
                //TestResult(message);
                // mTextView.setText("\n" + getString(R.string.SERVICEMENU_SAR_SENSOR_IS_SUCCESS)+"\n");
            //}
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    protected Dialog onCreateDialog(int none) {        
        return new AlertDialog.Builder(this)
            .setTitle(R.string.SERVICEMENU_DIALOG_TITLE_TXT)
            .setPositiveButton(R.string.SERVICEMENU_DIALOG_YES_TXT, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked OK so do some stuff */
                    finish();
                }
            })
            .setNegativeButton(R.string.SERVICEMENU_DIALOG_NO_TXT, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Cancel so do some stuff */
                    finish();
                }
            })
            .create();
    }

    @Override
    public void finish() {
        super.finish();
        tc.setTestResult(is_pass);

    }

    
    
 
    
    
    public boolean ac_ispass() {
        return is_pass;
    }

    private void TestResult() {
        String message = "";

        File mFile = new File("/mnt/sdcard/SARSensor.txt");
        //判断文件是否存在
        if (mFile.exists()) {
            //mFile.delete();
            return;
        }

        message =" \n" +" SARsensor is pass ";
        
        StringBuffer buffer = new StringBuffer();
        for(String value : valueList){
            buffer.append(value)
                    .append("  ");
        }
        try {
            //创建文件
            mFile.createNewFile();
            RandomAccessFile raf = new RandomAccessFile(mFile, "rwd");
            raf.seek(mFile.length());
            raf.write(buffer.toString().getBytes());
            raf.write(message.getBytes());
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
}